package com.spzx.cart.controller;

import com.spzx.cart.api.domain.CartInfo;
import com.spzx.cart.service.CartService;
import com.spzx.common.core.domain.R;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.security.annotation.InnerAuth;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: atguigu
 * @create: 2025-02-25 14:08
 */
@RestController
public class CartController {

    @Autowired
    private CartService cartService;

    @Operation(summary = "将指定商品加入到购物车")
    @GetMapping("/addToCart/{skuId}/{skuNum}")
    public AjaxResult addToCart(@PathVariable Long skuId, @PathVariable Integer skuNum) {
        cartService.addToCart(skuId, skuNum);
        return AjaxResult.success();
    }


    @Operation(summary = "查询当前用户购物车商品列表")
    @GetMapping("/cartList")
    public AjaxResult cartList() {
        List<CartInfo> list = cartService.cartList();
        return AjaxResult.success(list);
    }


    @Operation(summary = "删除购物车中指定商品")
    @DeleteMapping("/deleteCart/{skuId}")
    public AjaxResult deleteCart(@PathVariable Long skuId) {
        cartService.deleteCart(skuId);
        return AjaxResult.success();
    }

    /**
     * 修改购物车商品选中状态
     * @param skuId 商品ID
     * @param isChecked 1：选中  0：取消选中
     * @return
     */
    @Operation(summary = "修改购物车商品选中状态")
    @GetMapping("/checkCart/{skuId}/{isChecked}")
    public AjaxResult checkCart(@PathVariable Long skuId, @PathVariable Integer isChecked) {
        cartService.checkCart(skuId, isChecked);
        return AjaxResult.success();
    }

    @Operation(summary = "全选/取消选中")
    @GetMapping("/allCheckCart/{isChecked}")
    public AjaxResult allCheckCart(@PathVariable Integer isChecked) {
        cartService.allCheckCart(isChecked);
        return AjaxResult.success();
    }


    @Operation(summary = "清空购物车")
    @GetMapping("/clearCart")
    public AjaxResult clearCart() {
        cartService.clearCart();
        return AjaxResult.success();
    }



    @InnerAuth
    @Operation(summary = "查询用户购物车列表中选中商品列表")
    @GetMapping("/getCartCheckedList")
    public R<List<CartInfo>> getCartCheckedList(){
        List<CartInfo> list = cartService.getCartCheckedList();
        return R.ok(list);
    }

    @InnerAuth
    @Operation(summary="更新用户购物车列表价格")
    @GetMapping("/updateCartPrice/{userId}")
    public R<Boolean> updateCartPrice(@PathVariable(value = "userId",required = false) Long userId){
        return R.ok(cartService.updateCartPrice(userId));
    }

    @InnerAuth
    @Operation(summary="删除用户购物车列表中选中商品列表")
    @GetMapping("/deleteCartCheckedList/{userId}")
    public R<Boolean> deleteCartCheckedList(@PathVariable(value = "userId", required = false) Long userId){
        cartService.deleteCartCheckedList(userId);
        return R.ok();
    }

}
